package com.gxa.employment.config;

import com.gxa.employment.interceptor.AuthInterceptor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Description:
 * @Author:
 * @DateTime: 2025/3/18 14:35
 **/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    //拦截接口路径 17个
    private final String[] pathPatterns={"/clazz/**","/delay/**","/employment/**","/enterprise/**","/enterprise-employment/**","/ent-staff-job/**","/interview/**","/job/**","/major/**","/mgr/**","/permission/**","/resume/**","/role/**","/staff/**","/student/**","/stu-emp-detail/**","/img/**"};


    //注入拦截器
    private final AuthInterceptor authInterceptor;


    public WebMvcConfig(AuthInterceptor authInterceptor) {
        this.authInterceptor = authInterceptor;
    }

    /**
     * 拦截器设置，拦截哪些请求
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                //这里拦截什么需要自己添加
                .addPathPatterns(pathPatterns)
                .excludePathPatterns("/**/login","/**/register","/**/sms");
    }

    /**
     * 跨域请求设置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Value("${upload.path}")
    private String uploadPath;

    /**
     * 静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //这个是前端资源映射的路径
        registry.addResourceHandler("/upload/images/**"
                ) // 前端访问的URL路径
                .addResourceLocations("file:" + uploadPath + "/"); // 本地文件系统路径，注意添加 "file:"
        registry.addResourceHandler("/upload/resume/**")
                .addResourceLocations("file:./upload/resume/");

    }
}
